package com.example.demo.controller;

import com.example.demo.dto.EmployeeDTO;
import com.example.demo.entity.Employees;
import com.example.demo.service.EmployeesService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author PRE_38965
 */
@RestController
@RequestMapping("/demo")
public class EmployeesController {
    @Autowired
    EmployeesService employeesService;
    @PostMapping("/select")
    public List<Employees> queryAll(){
     List<Employees> employees=new ArrayList<>();
     employees=employeesService.queryAll();
        for(Employees employees1:employees){
            System.out.println(employees1);
        }
     return employees;
    }

    @PutMapping("/add")
    public String addEmployees(@RequestBody Employees employees){
        int i = employeesService.addEmployees(employees);
        return i>0?"success":"no";


    }
    @DeleteMapping("/del")
    public String del(Integer id){
        int i = employeesService.delEmployees(id);
        if (i == 1) {
            return "success";
        }else {
            return "删除失败了";
        }
    }
    @PostMapping("/update")
    public String upd(@RequestBody Employees employees){
        Integer integer = employeesService.updateEmployees(employees);
        if (integer == 1) {
            return "success";
        }else {
            return "修改失败了";
        }

    }
    @PostMapping("selectby")
    public String selectby(Integer id){
        Employees employees = employeesService.queryAllById(id);
        return employees.toString();
    }

    @PostMapping("/get-employee-with-leader")
    public ResponseEntity<?> getEmployeeWithLeader(@RequestBody EmployeeDTO employeeDTO) {
        System.out.println("miaxiajxijaxijaix");
        Employees employee = employeesService.getEmployeeWithLeader(employeeDTO);
        return ResponseEntity.ok(employee);
    }


}
